#include "ch02.h"

int main()
{
    FILE *stream;
    char buf[80];
    printf("open and create test_file\n");
    if((stream = fopen("test_file", "w")) == NULL)
    {
        err_exit("fopen failed");
    }
    printf("write string to test_file\n");
    // 输出字符串到文件流
    fputs("Hello world!", stream);
    if((stream = freopen("test_file", "r", stream)) == NULL)
    {
        err_exit("freopen failed");
    }
    printf("read string from test_file\n");
    // 从文件流读入一行字符串
    fgets(buf, sizeof(buf), stream);
    printf("the string is \"%s\"\n");
    fclose(stream);
    return 0;
}